package lt.liepasoft.sudoku;

public class Grid {

	public final int gridSize;
	protected int[] grid;
	
	public Grid(int gridSize) {
		this.gridSize = gridSize;
		grid = new int[gridSize*gridSize];
	}
	
	public int getCell(int x, int y){
		return grid[getCellIndex(x, y)];
	}
		
	public void setCell(int x, int y, int value){
		grid[getCellIndex(x, y)] = value; 
	}
	
	public int[] getRow(int rowIndex){
		int[] row = new int[gridSize];
		int index = getCellIndex(0, rowIndex);		
		for (int i=index, j=0; j<gridSize; i++, j++)
			row[j] = grid[i];		
		return row;
	}
	
	public int[] getCol(int colIndex){
		int[] col = new int[gridSize];
		for (int rowIndex=0; rowIndex<gridSize; rowIndex++) {
			col[rowIndex] = grid[getCellIndex(colIndex, rowIndex)];
		}
		return col;
	}

	public boolean isCellEmpty(int x, int y){
		return (grid[getCellIndex(x, y)] == 0);
	}	
    
	protected int getCellIndex(int x, int y){
        return (y * gridSize) + x;
    }
	
	protected int getRowIndex(int x, int y){
		return y;
	}
	
	protected int getColIndex(int x, int y){
		return x;
	}	
	
}
